package methods;

import java.awt.event.KeyEvent;
import java.io.IOException;

import methods.currentPlayer;


import com.rsbuddy.script.ActiveScript;
import com.rsbuddy.script.methods.Game;
import com.rsbuddy.script.methods.Keyboard;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.wrappers.Component;
import com.rsbuddy.script.wrappers.Widget;

public class connect extends ActiveScript{

	public static final int WIDGET_LOGIN = 596;
	public static final int WIDGET_LOGIN_IN = 59;
	public static final int WIDGET_LOGIN_ERROR = 68;
	public static final int WIDGET_LOGIN_EMAIL = 73;
	public static final int WIDGET_LOGIN_PASSWORD = 79;
	
	public static void doLogin() throws IOException{
		if(currentPlayer.acc == null){
			currentPlayer.selectPlayer();
		}else{
			if(Game.isLoginShowing()){
				if(Widgets.get(596).getComponent(68).containsText("Try Again")){
					Widgets.get(596).getComponent(68).click(true);
					sleep(2500);
				}else if(!Widgets.get(596).getComponent(73).containsText(currentPlayer.acc)){
					if(Widgets.get(596).getComponent(73).getText().isEmpty()){
						Widgets.get(596).getComponent(73).click(true);
						sleep(300, 400);
						Keyboard.sendText(currentPlayer.acc, false);
					}else if(!(Widgets.get(596).getComponent(73).getText().equals(currentPlayer.acc))){
						Widgets.get(596).getComponent(73).click(true);
						Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
						Keyboard.sendKey((char) KeyEvent.VK_DELETE);
					}
				}else{
					if(Widgets.get(596).getComponent(79).getText().isEmpty()){
						Widgets.get(596).getComponent(79).click(true);
						sleep(300, 400);
						Keyboard.sendText(currentPlayer.pass, true);
					}else if(!(Widgets.get(596).getComponent(79).getText().equals(currentPlayer.pass))){
						Widgets.get(596).getComponent(79).click(true);
						Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
						Keyboard.sendKey((char) KeyEvent.VK_DELETE);
					}
				}
				
			}
		}
	}
	
	public static boolean error(){
		Component w = getWidget().getComponent(WIDGET_LOGIN_ERROR);
		if(w.isVisible()){
		return w != null && w.click(true);
		}else{
			return false;
		}
	}
	
	public static boolean login (){
		Component w = getWidget().getComponent(WIDGET_LOGIN_IN);
		return w != null && w.click(true);
	}
	
	public static boolean delete(boolean x){
		if(x){
			if(getText(x).equals("")){
				return true;
			}
			getWidget().getComponent(WIDGET_LOGIN_EMAIL).click(true);
			Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
			Keyboard.sendKey((char) KeyEvent.VK_DELETE);
		}else{
			if(getText(x).equals("")){
				return true;
			}
			getWidget().getComponent(WIDGET_LOGIN_PASSWORD).click(true);
			Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
			Keyboard.sendKey((char) KeyEvent.VK_DELETE);
		}
		return false;
	}
	
	public static boolean write(boolean x){
		if(currentPlayer.acc == null){
			try {
				currentPlayer.selectPlayer();
			} catch (Exception e) {
				
			}
		}else if(x){
			if(getText(x).equals(currentPlayer.acc)){
				return true;
			}else if(getText(x).isEmpty()){
				getWidget().getComponent(WIDGET_LOGIN_EMAIL).click(true);
				sleep(300, 400);
				Keyboard.sendText(currentPlayer.acc, false);
			}else if(!getText(x).isEmpty() && !getText(x).equals(currentPlayer.acc)){
				getWidget().getComponent(WIDGET_LOGIN_EMAIL).click(true);
				Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
				Keyboard.sendKey((char) KeyEvent.VK_DELETE);
			}
		}else{
			if(!getText(x).equals("") && getText(x).length() == currentPlayer.pass.length()){
				return true;
			}else if(!getText(x).equals("") && !getText(x).equals(currentPlayer.pass)){
				getWidget().getComponent(WIDGET_LOGIN_PASSWORD).click(true);
				Keyboard.sendKey((char) KeyEvent.VK_BACK_SPACE);
				Keyboard.sendKey((char) KeyEvent.VK_DELETE);
			}else{
				getWidget().getComponent(WIDGET_LOGIN_PASSWORD).click(true);
				sleep(300, 400);
				Keyboard.sendText(currentPlayer.pass, false);
			}
		}
		return false;
	}
	
	public static String getText(boolean x){
		if(isOpen()){
			if(x){
				if(!getWidget().getComponent(WIDGET_LOGIN_EMAIL).getText().isEmpty()){
					return getWidget().getComponent(WIDGET_LOGIN_EMAIL).getText();
				}
				return "";
			}else{
				if(!getWidget().getComponent(WIDGET_LOGIN_PASSWORD).getText().isEmpty()){
					return getWidget().getComponent(WIDGET_LOGIN_PASSWORD).getText();
				}
				return "";
			}
		}
		return "";
	}
	
	public static boolean isOpen(){
		if(isValid()){
			return getWidget().getComponent(1).isVisible();
		}
		return false;
	}
	
	public static boolean isValid(){
		Widget w = getWidget();
		return w != null && w.isValid();
	}
	
	public static Widget getWidget(){
		return Widgets.get(WIDGET_LOGIN);
	}

	@Override
	public int loop() {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
